﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace DeleteUpdateSelfExe
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Form1 : Form
    {
        /// <summary>
        /// 
        /// </summary>
        public Form1()
        {
            InitializeComponent();

            this.Load += new EventHandler(Form1_Load);
        }

        /// <summary>
        /// 新文件后缀
        /// </summary>
        protected string FileName_Suffix
        {
            get
            {
                if (_fileName_Suffix == null)
                {
                    Guid gg = new Guid("{7F70DDCB-2CA6-45ff-B35E-5EE87ADFD91C}");
                    _fileName_Suffix=gg.ToString().Replace("-", "").Trim('{', '}');
                }
                return _fileName_Suffix;
            }
        }
        private string _fileName_Suffix;

        /// <summary>
        /// 当前文件
        /// </summary>
        protected string FileName_SelfPathName
        {
            get
            {
                string fileName = Application.ExecutablePath;
                return fileName;
            }
        }

        /// <summary>
        /// 当前文件仅文件名
        /// </summary>
        protected string FileName_SelfPathName_OnlyName
        {
            get
            {
                int index = FileName_SelfPathName.LastIndexOf('\\');
                string file_name = FileName_SelfPathName.Substring(index + 1);//exe文件全名(含扩展名)
                return file_name;
            }
        }

        /// <summary>
        /// 新文件全名
        /// </summary>
        protected string FileName_SelfPathName_OnlyName_New
        {
            get
            {
                //复制一个新文件
                int index = FileName_SelfPathName.LastIndexOf('\\');
                string file_path = FileName_SelfPathName.Substring(0, index);
                string file_name_1 = FileName_SelfPathName_OnlyName.Substring(0, FileName_SelfPathName_OnlyName.LastIndexOf('.'));
                string file_name_2 = FileName_SelfPathName_OnlyName.Substring(FileName_SelfPathName_OnlyName.LastIndexOf('.') + 1);
                string file_name_new = string.Format("{0}_{1}.{2}", file_name_1, FileName_Suffix, file_name_2);
                return file_name_new;

                //string fileNameNew = file_path.TrimEnd('\\') + '\\' + file_name_new;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        void Form1_Load(object sender, EventArgs e)
        {
            //显示现文件属性(时间)
            if (File.Exists(FileName_SelfPathName))
            {
                //
                FileInfo f = new FileInfo(FileName_SelfPathName);
                DateTime dt = f.LastWriteTime;
                TxtModifyTime.Text = dt.ToString("yyyy-MM-dd HH:mm:ss");
            }
        }

        /// <summary>
        /// 复制出一个新文件
        /// </summary>
        private void BtnCopy_Click(object sender, EventArgs e)
        {
            //复制一个新文件
            //int index = FileName_SelfPathName.LastIndexOf('\\');
            //string file_path = FileName_SelfPathName.Substring(0, index);
            //string file_name = FileName_SelfPathName_OnlyName;//FileName_SelfPathName.Substring(index + 1);//exe文件全名(含扩展名)
            //string file_name_1 = file_name.Substring(0, file_name.LastIndexOf('.'));
            //string file_name_2 = file_name.Substring(file_name.LastIndexOf('.') + 1);
            //string file_name_new = string.Format("{0}_{1}.{2}", file_name_1, FileName_Suffix, file_name_2);
            //string fileNameNew = file_path.TrimEnd('\\') + '\\' + file_name_new;

            //
            File.Copy(FileName_SelfPathName, FileName_SelfPathName_OnlyName_New, true);

            //更改新文件属性(时间)
            File.SetCreationTime(FileName_SelfPathName_OnlyName_New, DateTime.Now);
            File.SetLastWriteTime(FileName_SelfPathName_OnlyName_New, DateTime.Now);
            File.SetLastAccessTime(FileName_SelfPathName_OnlyName_New, DateTime.Now);

        }

        /// <summary>
        /// 退出并运行CMD
        /// </summary>
        private void BtnExitAndCmd_Click(object sender, EventArgs e)
        {
            //替换文件
            string cmdArgs = string.Format(
                "/C choice /t 1 /d y /n > nul & del {0} & rename {1} {2} & start {3}",
                FileName_SelfPathName_OnlyName,
                FileName_SelfPathName_OnlyName_New,
                FileName_SelfPathName_OnlyName,
                FileName_SelfPathName_OnlyName
                );
            ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", cmdArgs);
            psi.WindowStyle = ProcessWindowStyle.Hidden;
            psi.CreateNoWindow = true;
            Process.Start(psi);
            Application.Exit();
        }


    }
}
